package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.basic.DataDictionaryType;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryTypeService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.basic.DataDictionaryTypeInfo;
import com.qzdsoft.erpcloud.vo.query.basic.DataDictionaryTypeQueryParam;
//import com.qzdsoft.erpcloud.util.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.basic.BasicPreinstallEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/data/dictionary/type")
public class DataDictionaryTypeController extends BaseController{

//    @Autowired
//    private RedisHelper redisHelper;

    @Autowired
    CenterCompanyService actCenterCompanyService;
    
    @Autowired
    private DataDictionaryTypeService basicDataDictionaryTypeService;

    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        model.addAttribute("preinstallList", preinstallList);
        model.addAttribute("preinstallMap", preinstallMap);

        model.addAttribute("actCenterCompanyMap",actCenterCompanyService.getAllCompany());

        return "basic/data_dictionary/index";
    }

    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest request, @RequestParam("id") Integer id) {
        ModelAndView mv = new ModelAndView();
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        mv.addObject("preinstallList", preinstallList);
        mv.addObject("preinstallMap", preinstallMap);

        mv.addObject("actCenterCompanyMap",actCenterCompanyService.getAllCompany());
        
        if(null != id){
//            return Response.getSuccess(basicDataDictionaryTypeService.selectById(id));
           DataDictionaryType dataDictionaryType =basicDataDictionaryTypeService.selectById(id);
           mv.addObject("dataDictionaryType", dataDictionaryType);
           mv.setViewName("/basic/data_dictionary/edit");
        }else {
            mv.setViewName("/basic/data_dictionary/add");
        }
        return mv;
    }

    /**
     * 编辑保存
     * @param request
     * @param basicDataDictionaryType
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑字典类型保存")
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request, DataDictionaryType basicDataDictionaryType) {
//        basicDataDictionaryType.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicDataDictionaryType.getCompany_id()));
        return basicDataDictionaryTypeService.saveOrUpd(basicDataDictionaryType);
    }
    
    /**
     * 新增保存
     * @param request
     * @param basicDataDictionaryType
     * @return
     */
    @ResponseBody
    @SysLog(description="新增字典类型保存")
    @RequestMapping("/create_execute")
    public Object createExecute(HttpServletRequest request, DataDictionaryType basicDataDictionaryType) {
//        basicDataDictionaryType.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicDataDictionaryType.getCompany_id()));
        return basicDataDictionaryTypeService.saveOrUpd(basicDataDictionaryType);
    }


    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除字典类型")
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicDataDictionaryTypeService.del(id);
    }

    @ResponseBody
    @SysLog(description="查询字典类型")
    @RequestMapping("/data")
    public LayTableResponse<DataDictionaryTypeInfo> data(HttpServletRequest request, DataDictionaryTypeQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
//        if(null != companyId){
//            param.setCompanyId(companyId);
//        }

        return basicDataDictionaryTypeService.selectData(param.getName(), param.getDecode(), param.getCompanyId(), param.getIsPreinstall(), param.getPage(), param.getLimit());
    }

}
